package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.easycarebn.main.utils.Utils;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.timbacsi.main.R;

public class FragmentMapContact extends Fragment {
    public static final String FRAGMENT_TAG = "RESULT_MAP";

    private View mView;
    private static FragmentMapContact mFragmentResultMap = null;
    private FragmentActivity mFragmentActivity;
    private GoogleMap mMap;

    private LatLng location = null;

    public static FragmentMapContact getInstance(Bundle data) {
        if (mFragmentResultMap == null) {
            mFragmentResultMap = new FragmentMapContact();
        }
        mFragmentResultMap.setArguments(data);
        return mFragmentResultMap;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof FragmentActivity) {
            mFragmentActivity = (FragmentActivity) activity;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        location = FragmentResultMap.getlatLng(getArguments().getString(Utils.KEY_LOCATION));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_result_map, null);
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mMap = ((SupportMapFragment) mFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker marker) { // TODO
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) { // TODO
                View v = mFragmentActivity.getLayoutInflater().inflate(R.layout.map_info_contact, null);
                TextView tv_address = (TextView) v.findViewById(R.id.tv_address);
                tv_address.setText(getArguments().getString(Utils.KEY_ADRESS));

                return v;
            }
        });

        mMap.addMarker(new MarkerOptions().position(location).icon(
                BitmapDescriptorFactory.fromResource(R.drawable.ic_location_map)));
        // Move the camera instantly to hamburg with a zoom of 15.
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));

        // Zoom in, animating the camera.
        mMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
        super.onActivityCreated(savedInstanceState);
    }
}
